package com.wy._11NumberEqu;

import com.wy._10functionUnit.People;

import java.math.BigDecimal;

public class NumberEqu {

    //数据的比较。如果是一个对象的话，但是实际可能是一个数字。
    public static boolean areEqual(Object obj1, Object obj2) {
        BigDecimal value1 = convertToBigDecimal(obj1);
        BigDecimal value2 = convertToBigDecimal(obj2);

        // 如果任一转换失败，则返回false
        if (value1 == null || value2 == null) {
            return false;
        }

        // 移除尾随零后进行比较
        return value1.stripTrailingZeros().compareTo(value2.stripTrailingZeros()) == 0;
    }

    private static BigDecimal convertToBigDecimal(Object obj) {
        if (obj instanceof Number) {
            return new BigDecimal(obj.toString());
        } else if (obj instanceof Character) {
            char ch = (Character) obj;
            if (Character.isDigit(ch)) {
                return new BigDecimal(String.valueOf(ch));
            }
        } else if (obj instanceof String) {
            try {
                return new BigDecimal((String) obj);
            } catch (NumberFormatException e) {
                // 如果字符串不是有效的数字，则返回null
                return null;
            }
        }
        return null;
    }


    public static void main(String[] args) {
        Object obj1 = "8";
        Object obj2 = "8.00";

        System.out.println(areEqual(obj1, obj2));  // 输出: true
    }
}
